"""
 -*- coding: utf-8 -*-
 @Time : 2023/11/21 15:10
 @Author : 支晓光
 @Email : 1473858993@qq.com
 @File : base_page.py
 @Project : youguuUI
 """
import os
import time
from selenium.webdriver.support.ui import WebDriverWait

from common.handler_path import screenshots_dir


class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, loc):  # 获取一个元素
        return self.driver.find_element(*loc)

    def click_ele(self, loc):  # 点击
        self.find_element(loc).click()

    def send_keys(self, loc, txt):  # 输入
        self.find_element(loc).send_keys(txt)

    def find_elements(self, loc):  # 获取一组元素
        return self.driver.find_elements(*loc)

    def find_wait(self, loc, time=3):  # 显示等待
        return WebDriverWait(self.driver, time).until(lambda d: d.find_element(*loc), message='未找到元素')

    def find_waits(self, loc, time=3):  # 显示等待
        return WebDriverWait(self.driver, time).until(lambda d: d.find_elements(*loc), message='未找到元素组')

    def tap(self, x1, y1):  # 轻敲
        a = x1 / 1080
        b = y1 / 1920
        w = self.driver.get_window_size()['width']
        h = self.driver.get_window_size()['height']
        self.driver.tap([(a * w, b * h)])

    def swip_left(self, x):  # 左滑
        for i in range(x):
            w = self.driver.get_window_size()['width']
            h = self.driver.get_window_size()['height']
            self.driver.swipe(w * 0.9, h * 0.9, w * 0.1, h * 0.9)

    def swip_right(self, x):  # 右滑
        for i in range(x):
            w = self.driver.get_window_size()['width']
            h = self.driver.get_window_size()['height']
            self.driver.swipe(w * 0.1, h * 0.9, w * 0.9, h * 0.9)

    def swip_up(self, x):  # 上划滑
        for i in range(x):
            w = self.driver.get_window_size()['width']
            h = self.driver.get_window_size()['height']
            self.driver.swipe(w * 0.9, h * 0.9, w * 0.9, h * 0.1)

    def swip_down(self, x):  # 下滑
        for i in range(x):
            w = self.driver.get_window_size()['width']
            h = self.driver.get_window_size()['height']
            self.driver.swipe(w * 0.9, h * 0.1, w * 0.9, h * 0.9)

    def screenshot(self, name):  # 截图
        ntime = time.strftime("%Y%m%d%H%M%S", time.localtime())
        filepath = os.path.join(screenshots_dir, os.path.sep, '{}_{}.png'.format(name, ntime))
        self.driver.save_screenshot(filepath)

    def over(self):  # 结束进程
        self.driver.quit()
